// os/src/console.rs
use crate::sbi;
use core::fmt::{self, Write};

struct Stdout;

impl Write for Stdout {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        for c in s.chars() {
            sbi::console_putchar(c as usize);
        }
        Ok(())
    }
}

pub fn print(args: fmt::Arguments) {
    Stdout.write_fmt(args).unwrap();
}

#[macro_export]
macro_rules! print {
    ($fmt: literal $(, $($arg: tt)+)?) => {
        $crate::console::print(format_args!($fmt $(, $($arg)+)?));
    }
}

#[macro_export]
macro_rules! println {
    ($fmt: literal $(, $($arg: tt)+)?) => {
        $crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
    }
}

#[macro_export]
macro_rules! info {
    // 蓝色(96)
    ($fmt: literal $(, $($arg: tt)+)?) => {
        $crate::console::print(format_args!(concat!("\x1b[96m", $fmt, "\n", "\x1b[0m") $(, $($arg)+)?));
    }
}

#[macro_export]
macro_rules! error {
    // 红色(	91)
    ($fmt: literal $(, $($arg: tt)+)?) => {
        $crate::console::print(format_args!(concat!("\x1b[91m", $fmt, "\n", "\x1b[0m") $(, $($arg)+)?));
    }
}

#[macro_export]
macro_rules! debug {
    // 绿色(92)
    ($fmt: literal $(, $($arg: tt)+)?) => {
        $crate::console::print(format_args!(concat!("\x1b[92m", $fmt, "\n", "\x1b[0m") $(, $($arg)+)?));
    }
}

/*
warn:
    表示发生不常见情况，但是并不一定导致系统错误
 */
#[macro_export]
macro_rules! warn {
    // 黄色(93)
    ($fmt: literal $(, $($arg: tt)+)?) => {
        $crate::console::print(format_args!(concat!("\x1b[93m", $fmt, "\n", "\x1b[0m") $(, $($arg)+)?));
    }
}

/*
trace:
    最详细的输出, 跟踪了每一步关键路径的执行.
 */
#[macro_export]
macro_rules! trace {
    // 亮白(97)
    ($fmt: literal $(, $($arg: tt)+)?) => {
        $crate::console::print(format_args!(concat!("\x1b[97m", $fmt, "\n", "\x1b[0m") $(, $($arg)+)?));
    }
}